from django.db import models

# Create your models here.

class Student(models.Model):

    name = models.CharField(max_length=20, verbose_name='姓名')
    age = models.SmallIntegerField('年龄')
    sex = models.SmallIntegerField(default=1, verbose_name='性别', choices=((0, '女'), (1, '男')))
    qq = models.CharField(max_length=20, unique=True, verbose_name='QQ')
    mobile = models.CharField('电话', max_length=20, unique=True, error_messages={'unique': '已存在的电话号码'})
    webchat = models.CharField('微信', max_length=20, unique=True, error_messages={'unique': '已存在的微信号码'})
    c_time = models.DateTimeField('创建时间', auto_now_add=True)
    e_time = models.DateTimeField('修改时间', auto_now=True)
    grade = models.ForeignKey('Grade', on_delete=models.SET_NULL, null=True)
    is_delete = models.BooleanField(default=False)

    def __str__(self):

        return '{}-{}'.format(self.name, self.age, self.sex)

class StudentDetail(models.Model):

    num = models.CharField('身份证号码', max_length=18, unique=True)
    collage = models.CharField(max_length=20, default='')
    student = models.OneToOneField('Student', on_delete=models.CASCADE, related_name='student')

class Grade(models.Model):

    name = models.CharField(max_length=20)
    num = models.CharField(max_length=20)

    def __str__(self):

        return '{}-{}'.format(self.name, self.num)

class Course(models.Model):

    name = models.CharField(max_length=20)
    student = models.ManyToManyField('Student', through='Enroll')

    def __str__(self):

        return self.name

class Enroll(models.Model):

    student = models.ForeignKey('Student', on_delete=models.CASCADE)
    course = models.ForeignKey('Course', on_delete=models.CASCADE)
    pay = models.FloatField('缴费金额', default=0)
    c_time = models.DateTimeField('报名时间', auto_now_add=True)